<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta name="referrer" content="no-referrer"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>项目中用到的工厂模式 | Welcome,Guys,Join Me for the AIminminAI Episode!</title><meta name="author" content="Hu hb"><meta name="copyright" content="Hu hb"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="@TOC  前言做项目的时候，用到了设计模式，总结下来，供以后参考，与诸君共勉。 一、代码中的部分片段123456789101112131415161718192021222324using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks">
<meta property="og:type" content="article">
<meta property="og:title" content="项目中用到的工厂模式">
<meta property="og:url" content="https://aiminminai.gitee.io/aiminminai/2023/08/20/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E7%94%A8%E5%88%B0%E7%9A%84%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/index.html">
<meta property="og:site_name" content="Welcome,Guys,Join Me for the AIminminAI Episode!">
<meta property="og:description" content="@TOC  前言做项目的时候，用到了设计模式，总结下来，供以后参考，与诸君共勉。 一、代码中的部分片段123456789101112131415161718192021222324using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://aiminminai.gitee.io/aiminminai/img/avatar.jpg">
<meta property="article:published_time" content="2023-08-20T14:33:20.000Z">
<meta property="article:modified_time" content="2023-10-22T13:53:11.621Z">
<meta property="article:author" content="Hu hb">
<meta property="article:tag" content="项目开发经验总结">
<meta property="article:tag" content="设计模式">
<meta property="article:tag" content="工厂模式">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://aiminminai.gitee.io/aiminminai/img/avatar.jpg"><link rel="shortcut icon" href="/aiminminai/img/favicon.png"><link rel="canonical" href="https://aiminminai.gitee.io/aiminminai/2023/08/20/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E7%94%A8%E5%88%B0%E7%9A%84%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/aiminminai/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/aiminminai/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: {"limitCount":50,"languages":{"author":"Author: Hu hb","link":"Link: ","source":"Source: Welcome,Guys,Join Me for the AIminminAI Episode!","info":"Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source."}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '项目中用到的工厂模式',
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2023-10-22 21:53:11'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/aiminminai/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/aiminminai/archives/"><div class="headline">Articles</div><div class="length-num">29</div></a><a href="/aiminminai/tags/"><div class="headline">Tags</div><div class="length-num">47</div></a><a href="/aiminminai/categories/"><div class="headline">Categories</div><div class="length-num">42</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/aiminminai/"><i class="fa-fw fas fa-home"></i><span> Home(主页)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives(那些年的知识归档)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags(那些年的知识标签)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories(那些年的工作经验)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/link/"><i class="fa-fw fas fa-link"></i><span> Link(那些年的八股)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/game/"><i class="fa-fw fas fa-heart"></i><span> Game Area(游戏专区，学&amp;玩)</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/aiminminai/img/JingSeOne.jpg')"><nav id="nav"><span id="blog-info"><a href="/aiminminai/" title="Welcome,Guys,Join Me for the AIminminAI Episode!"><span class="site-name">Welcome,Guys,Join Me for the AIminminAI Episode!</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/aiminminai/"><i class="fa-fw fas fa-home"></i><span> Home(主页)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives(那些年的知识归档)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags(那些年的知识标签)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories(那些年的工作经验)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/link/"><i class="fa-fw fas fa-link"></i><span> Link(那些年的八股)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/game/"><i class="fa-fw fas fa-heart"></i><span> Game Area(游戏专区，学&amp;玩)</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">项目中用到的工厂模式</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-08-20T14:33:20.000Z" title="Created 2023-08-20 22:33:20">2023-08-20</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-10-22T13:53:11.621Z" title="Updated 2023-10-22 21:53:11">2023-10-22</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/aiminminai/categories/%E9%A1%B9%E7%9B%AE/">项目</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/aiminminai/categories/%E9%A1%B9%E7%9B%AE/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%88%B0%E7%9A%84%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">项目中使用到的设计模式</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/aiminminai/categories/%E9%A1%B9%E7%9B%AE/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%88%B0%E7%9A%84%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E7%94%A8%E5%88%B0%E7%9A%84%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/">项目中用到的工厂模式</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="项目中用到的工厂模式"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>@<a href="%E6%96%87%E7%AB%A0%E7%9B%AE%E5%BD%95">TOC</a></p>
<hr>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>做项目的时候，用到了设计模式，总结下来，供以后参考，与诸君共勉。</p>
<h1 id="一、代码中的部分片段"><a href="#一、代码中的部分片段" class="headerlink" title="一、代码中的部分片段"></a>一、代码中的部分片段</h1><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-keyword">using</span> System;<br><span class="hljs-keyword">using</span> System.Collections.Generic;<br><span class="hljs-keyword">using</span> System.Linq;<br><span class="hljs-keyword">using</span> System.Text;<br><span class="hljs-keyword">using</span> System.Threading.Tasks;<br><br><span class="hljs-keyword">namespace</span> <span class="hljs-title">WindowsFormsCustomInfo</span><br>&#123;<br>    <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title">StepFactory</span><br>    &#123;<br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> IStep <span class="hljs-title">GetStep</span>(<span class="hljs-params"><span class="hljs-built_in">int</span> stepIndex</span>)</span><br>        &#123;<br>            <span class="hljs-keyword">return</span> _StepMap.ContainsKey(stepIndex) ? _StepMap[stepIndex] : <span class="hljs-literal">null</span>;<br>        &#125;<br><br>        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">readonly</span> Dictionary&lt;<span class="hljs-built_in">int</span>, IStep&gt; _StepMap = <span class="hljs-keyword">new</span> Dictionary&lt;<span class="hljs-built_in">int</span>, IStep&gt;()<br>        &#123;<br>            &#123;<span class="hljs-number">1</span>,<span class="hljs-keyword">new</span> MyStep1() &#125;,<br>            &#123;<span class="hljs-number">2</span>,<span class="hljs-keyword">new</span> MyStep2() &#125;,<br>            &#123;<span class="hljs-number">3</span>,<span class="hljs-keyword">new</span> MyStep3() &#125;,<br>        &#125;;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h1 id="二、既然用到了，那么Head-First系列或者设计模式之禅，不得拉出来掰扯掰扯嘛"><a href="#二、既然用到了，那么Head-First系列或者设计模式之禅，不得拉出来掰扯掰扯嘛" class="headerlink" title="二、既然用到了，那么Head First系列或者设计模式之禅，不得拉出来掰扯掰扯嘛"></a>二、既然用到了，那么Head First系列或者设计模式之禅，不得拉出来掰扯掰扯嘛</h1><h2 id="1-使用new关键字进行实例化对象时是针对实现编程【相当于用new面向实现编程，让代码绑定了具体类，如下代码】，应该面向接口而不是面向实现，"><a href="#1-使用new关键字进行实例化对象时是针对实现编程【相当于用new面向实现编程，让代码绑定了具体类，如下代码】，应该面向接口而不是面向实现，" class="headerlink" title="1.使用new关键字进行实例化对象时是针对实现编程【相当于用new面向实现编程，让代码绑定了具体类，如下代码】，应该面向接口而不是面向实现，"></a>1.使用new关键字进行实例化对象时是针对实现编程【相当于用new面向实现编程，让代码绑定了具体类，如下代码】，应该面向接口而不是面向实现，</h2><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-comment">//使用new关键字进行实例化对象时是针对实现编程【相当于用new面向实现编程，让代码绑定了具体类，如下代码】，应该面向接口而不是面向实现</span><br>    ...<br>    IStep currentStep;    <br>        <span class="hljs-keyword">if</span> (stepIndex == <span class="hljs-number">1</span>)<br>            &#123;<br>                currentStep = <span class="hljs-keyword">new</span> MyStep1();<br>            &#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(stepIndex == <span class="hljs-number">2</span>)<br>            &#123;<br>                currentStep = <span class="hljs-keyword">new</span> MyStep2();<br>            &#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(stepIndex == <span class="hljs-number">3</span>)<br>            &#123;<br>                currentStep = <span class="hljs-keyword">new</span> MyStep3();<br>            &#125;...<span class="hljs-comment">//要实例化哪个类要在运行时由判断条件决定，一旦有变化或扩展就必须打开这段代码进行检查和修改，易出错还难维护和修改</span><br>    ...<br></code></pre></td></tr></table></figure>
<ul>
<li>违反了对扩展开放，对修改关闭：后期如果有新步骤要加入，而有些旧步骤不要了要删掉，那么就得改动这里多重分支判断<ul>
<li>解决办法：找到变动的地方和不变的地方，不变的地方就是每个步骤要显示自己的步骤索引、调用IsLastStep判断是否是最后一步等，变动的就是if…else…中多个步骤对象实例化的部分。那么具体思路就是把变动的这部分单独放出去，把创建步骤的代码移到另一个对象中由这个新对象专职创建步骤对象，其他任何对象想要创建步骤就找这个对象就行，这个新对象就是工厂</li>
</ul>
</li>
</ul>
<h2 id="2-简单工厂："><a href="#2-简单工厂：" class="headerlink" title="2.简单工厂："></a>2.简单工厂：</h2><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">SimpleStepFactory</span><br>    &#123;<br>        <span class="hljs-function"><span class="hljs-keyword">public</span> IStep <span class="hljs-title">CreateStep</span>(<span class="hljs-params"><span class="hljs-built_in">string</span> stepIndex</span>)</span><br>        &#123;<br>            IStep currentStep = <span class="hljs-literal">null</span>; <br>            <span class="hljs-keyword">if</span> (stepIndex == <span class="hljs-number">1</span>)<span class="hljs-comment">//也可以if(type.equals(&quot;MyStep1&quot;))&#123;...&#125;</span><br>            &#123;<br>                currentStep = <span class="hljs-keyword">new</span> MyStep1();<br>            &#125;<br>            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (stepIndex == <span class="hljs-number">2</span>)<br>            &#123;<br>                currentStep = <span class="hljs-keyword">new</span> MyStep2();<br>            &#125;<br>            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (stepIndex == <span class="hljs-number">3</span>)<br>            &#123;<br>                currentStep = <span class="hljs-keyword">new</span> MyStep3();<br>            &#125;<br>            <span class="hljs-keyword">return</span> currentStep;<br><br>        &#125;<br>    &#125;<br></code></pre></td></tr></table></figure>
<ul>
<li>符合单一职责，所有用户都用这个CreateStep方法来实例化新对象</li>
</ul>
<h2 id="3-静态工厂：利用静态方法定义一个简单的工厂，优点就是不需要使用创建对象的方法来实例化对象，但是缺点是不能通过继承来改变创建对象的行为"><a href="#3-静态工厂：利用静态方法定义一个简单的工厂，优点就是不需要使用创建对象的方法来实例化对象，但是缺点是不能通过继承来改变创建对象的行为" class="headerlink" title="3.静态工厂：利用静态方法定义一个简单的工厂，优点就是不需要使用创建对象的方法来实例化对象，但是缺点是不能通过继承来改变创建对象的行为"></a>3.静态工厂：利用静态方法定义一个简单的工厂，优点就是不需要使用创建对象的方法来实例化对象，但是缺点是不能通过继承来改变创建对象的行为</h2><h2 id="4-除了使用new操作符之外，还可以通过反射、工厂模式等方法制造对象实例"><a href="#4-除了使用new操作符之外，还可以通过反射、工厂模式等方法制造对象实例" class="headerlink" title="4.除了使用new操作符之外，还可以通过反射、工厂模式等方法制造对象实例"></a>4.除了使用new操作符之外，还可以通过反射、工厂模式等方法制造对象实例</h2><hr>
<h1 id="巨人的肩膀"><a href="#巨人的肩膀" class="headerlink" title="巨人的肩膀"></a>巨人的肩膀</h1><ul>
<li>Head First 设计模式</li>
<li>设计模式之禅</li>
<li>公众号啦、OSCHINA啦、上面的有关设计模式的文章</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://aiminminai.gitee.io/aiminminai">Hu hb</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://aiminminai.gitee.io/aiminminai/2023/08/20/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E7%94%A8%E5%88%B0%E7%9A%84%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/">https://aiminminai.gitee.io/aiminminai/2023/08/20/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E7%94%A8%E5%88%B0%E7%9A%84%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/aiminminai/tags/%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E7%BB%8F%E9%AA%8C%E6%80%BB%E7%BB%93/">项目开发经验总结</a><a class="post-meta__tags" href="/aiminminai/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a><a class="post-meta__tags" href="/aiminminai/tags/%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/">工厂模式</a></div><div class="post_share"><div class="social-share" data-image="/aiminminai/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> Donate</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/aiminminai/img/WeChat.jpg" target="_blank"><img class="post-qr-code-img" src="/aiminminai/img/WeChat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/aiminminai/img/Alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/aiminminai/img/Alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/aiminminai/2023/08/20/C-%E7%BC%96%E7%A8%8B%E7%BB%8F%E9%AA%8C-%E9%81%87%E5%88%B0%E9%99%B7%E9%98%B1%E6%80%BB%E7%BB%93-%E4%B8%89/" title="C#编程经验/遇到陷阱总结(三)"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">C#编程经验/遇到陷阱总结(三)</div></div></a></div><div class="next-post pull-right"><a href="/aiminminai/2023/08/20/C-%E7%BC%96%E7%A8%8B%E7%BB%8F%E9%AA%8C-%E9%81%87%E5%88%B0%E9%99%B7%E9%98%B1%E6%80%BB%E7%BB%93-%E4%BA%8C/" title="C#编程经验/遇到陷阱总结(二)"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">C#编程经验/遇到陷阱总结(二)</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/aiminminai/2023/10/22/%E4%BD%A0Java%E6%9C%89MVC-%E6%88%91C-%E6%9C%89MVVM-02-MVVM%E7%9A%84%E4%B8%80%E4%B8%AA%E5%B0%8F%E7%9A%84%E6%88%90%E7%86%9F%E7%9A%84%E7%A8%B3%E5%AE%9A%E7%9A%84%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/" title="你Java有MVC,我C#有MVVM(02:MVVM的一个小的成熟的稳定的项目应用)"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-22</div><div class="title">你Java有MVC,我C#有MVVM(02:MVVM的一个小的成熟的稳定的项目应用)</div></div></a></div><div><a href="/aiminminai/2023/10/22/%E4%BD%A0Java%E6%9C%89MVC%EF%BC%8C%E6%88%91C-%E6%9C%89MVVM-01/" title="你Java有MVC，我C#有MVVM(01)"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-22</div><div class="title">你Java有MVC，我C#有MVVM(01)</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/aiminminai/img/avatar.jpg" onerror="this.onerror=null;this.src='/aiminminai/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Hu hb</div><div class="author-info__description">寻寻觅觅，原来未来的你，在这里呀</div></div><div class="card-info-data site-data is-center"><a href="/aiminminai/archives/"><div class="headline">Articles</div><div class="length-num">29</div></a><a href="/aiminminai/tags/"><div class="headline">Tags</div><div class="length-num">47</div></a><a href="/aiminminai/categories/"><div class="headline">Categories</div><div class="length-num">42</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/aiminminai"><i class="Gitee"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:min221forever@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://github.com/YWMHHBAIminminAI" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/m0_52436398?type=blog" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="https://gitee.com/aiminminai" target="_blank" title="Gitee"><i class="fa fa-g"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content"><b><font color="#e66b6d">掬</font> <font color="#e66d98">水</font> <font color="#e66cc6">月</font> <font color="#cc6de6">在</font> <font        color="#9770e6">手</font> <font color="#6d93e6">，</font> <font color="#6fcde6">弄</font> <font color="#72e6b6">花</font> <font color="#72e689">香</font> <font color="#99e670">满</font> <font color="#cde670">衣</font> <p align="center"><img src="https://haiyong.site/img/img-blog.csdnimg.cn/f7384c88956d4378b72e47548e19c9f8.gif" width="50" alt="mao"></p> <p align="center">微信号：phone_15902953075</p> <p align="center">QQ号：1317957931</p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E4%BB%A3%E7%A0%81%E4%B8%AD%E7%9A%84%E9%83%A8%E5%88%86%E7%89%87%E6%AE%B5"><span class="toc-number">2.</span> <span class="toc-text">一、代码中的部分片段</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%97%A2%E7%84%B6%E7%94%A8%E5%88%B0%E4%BA%86%EF%BC%8C%E9%82%A3%E4%B9%88Head-First%E7%B3%BB%E5%88%97%E6%88%96%E8%80%85%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8B%E7%A6%85%EF%BC%8C%E4%B8%8D%E5%BE%97%E6%8B%89%E5%87%BA%E6%9D%A5%E6%8E%B0%E6%89%AF%E6%8E%B0%E6%89%AF%E5%98%9B"><span class="toc-number">3.</span> <span class="toc-text">二、既然用到了，那么Head First系列或者设计模式之禅，不得拉出来掰扯掰扯嘛</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E4%BD%BF%E7%94%A8new%E5%85%B3%E9%94%AE%E5%AD%97%E8%BF%9B%E8%A1%8C%E5%AE%9E%E4%BE%8B%E5%8C%96%E5%AF%B9%E8%B1%A1%E6%97%B6%E6%98%AF%E9%92%88%E5%AF%B9%E5%AE%9E%E7%8E%B0%E7%BC%96%E7%A8%8B%E3%80%90%E7%9B%B8%E5%BD%93%E4%BA%8E%E7%94%A8new%E9%9D%A2%E5%90%91%E5%AE%9E%E7%8E%B0%E7%BC%96%E7%A8%8B%EF%BC%8C%E8%AE%A9%E4%BB%A3%E7%A0%81%E7%BB%91%E5%AE%9A%E4%BA%86%E5%85%B7%E4%BD%93%E7%B1%BB%EF%BC%8C%E5%A6%82%E4%B8%8B%E4%BB%A3%E7%A0%81%E3%80%91%EF%BC%8C%E5%BA%94%E8%AF%A5%E9%9D%A2%E5%90%91%E6%8E%A5%E5%8F%A3%E8%80%8C%E4%B8%8D%E6%98%AF%E9%9D%A2%E5%90%91%E5%AE%9E%E7%8E%B0%EF%BC%8C"><span class="toc-number">3.1.</span> <span class="toc-text">1.使用new关键字进行实例化对象时是针对实现编程【相当于用new面向实现编程，让代码绑定了具体类，如下代码】，应该面向接口而不是面向实现，</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%EF%BC%9A"><span class="toc-number">3.2.</span> <span class="toc-text">2.简单工厂：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E9%9D%99%E6%80%81%E5%B7%A5%E5%8E%82%EF%BC%9A%E5%88%A9%E7%94%A8%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E5%B7%A5%E5%8E%82%EF%BC%8C%E4%BC%98%E7%82%B9%E5%B0%B1%E6%98%AF%E4%B8%8D%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95%E6%9D%A5%E5%AE%9E%E4%BE%8B%E5%8C%96%E5%AF%B9%E8%B1%A1%EF%BC%8C%E4%BD%86%E6%98%AF%E7%BC%BA%E7%82%B9%E6%98%AF%E4%B8%8D%E8%83%BD%E9%80%9A%E8%BF%87%E7%BB%A7%E6%89%BF%E6%9D%A5%E6%94%B9%E5%8F%98%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E8%A1%8C%E4%B8%BA"><span class="toc-number">3.3.</span> <span class="toc-text">3.静态工厂：利用静态方法定义一个简单的工厂，优点就是不需要使用创建对象的方法来实例化对象，但是缺点是不能通过继承来改变创建对象的行为</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E9%99%A4%E4%BA%86%E4%BD%BF%E7%94%A8new%E6%93%8D%E4%BD%9C%E7%AC%A6%E4%B9%8B%E5%A4%96%EF%BC%8C%E8%BF%98%E5%8F%AF%E4%BB%A5%E9%80%9A%E8%BF%87%E5%8F%8D%E5%B0%84%E3%80%81%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E7%AD%89%E6%96%B9%E6%B3%95%E5%88%B6%E9%80%A0%E5%AF%B9%E8%B1%A1%E5%AE%9E%E4%BE%8B"><span class="toc-number">3.4.</span> <span class="toc-text">4.除了使用new操作符之外，还可以通过反射、工厂模式等方法制造对象实例</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B7%A8%E4%BA%BA%E7%9A%84%E8%82%A9%E8%86%80"><span class="toc-number">4.</span> <span class="toc-text">巨人的肩膀</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/28/%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E6%80%BB%E7%BB%93%E4%B9%8BC-%E5%86%99%E4%BA%86%E4%B8%80%E4%B8%AA%E4%B8%8A%E4%BD%8D%E6%9C%BA%E9%A1%B5%E9%9D%A2%EF%BC%8C%E8%BD%AF%E4%BB%B6%E5%8D%A1%E6%AD%BB%E4%BA%86%EF%BC%9F%EF%BC%9F%EF%BC%9F/" title="项目开发总结之C#写了一个上位机页面，软件卡死了？？？">项目开发总结之C#写了一个上位机页面，软件卡死了？？？</a><time datetime="2024-03-28T15:41:40.000Z" title="Created 2024-03-28 23:41:40">2024-03-28</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/27/Golang-web-http%E6%A1%86%E6%9E%B6/" title="Golang-web/http框架">Golang-web/http框架</a><time datetime="2024-03-27T15:27:12.000Z" title="Created 2024-03-27 23:27:12">2024-03-27</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/24/Golang-%E7%BB%83%E5%9F%BA%E6%9C%AC%E5%8A%9F%EF%BC%9A%E6%89%8E%E9%A9%AC%E6%AD%A5%E3%80%81%E7%BB%83%E6%8A%95%E7%AF%AE/" title="Golang-练基本功：扎马步、练投篮、练运球">Golang-练基本功：扎马步、练投篮、练运球</a><time datetime="2024-03-24T15:42:23.000Z" title="Created 2024-03-24 23:42:23">2024-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/15/%E8%83%BD%E4%B8%8D%E8%83%BD%E7%BB%99%E5%88%AB%E4%BA%BA%E4%B8%80%E4%B8%AA%E4%B8%8A%E6%89%8B%E5%B0%B1%E8%83%BD%E7%9C%8B%E6%87%82%E5%B0%B1%E8%83%BD%E7%94%A8%E7%9A%84%E4%B8%89%E5%B1%82%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%EF%BC%88%E4%BB%A3%E7%A0%81%EF%BC%89%E6%A8%A1%E5%9E%8B%E5%91%A2%EF%BC%9F/" title="能不能给别人一个上手就能看懂就能用的三层神经网络（代码）模型呢？">能不能给别人一个上手就能看懂就能用的三层神经网络（代码）模型呢？</a><time datetime="2024-03-14T16:23:21.000Z" title="Created 2024-03-15 00:23:21">2024-03-15</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/02/28/%E6%89%93%E7%A0%B4%E7%A0%82%E9%94%85%E9%97%AE-WPF%E5%8E%9F%E7%90%86-03-WPF%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96%E3%80%81%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF/" title="打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板">打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板</a><time datetime="2024-02-28T15:10:28.000Z" title="Created 2024-02-28 23:10:28">2024-02-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Hu hb</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="chat-btn" type="button" title="Chat"><i class="fas fa-sms"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/aiminminai/js/utils.js"></script><script src="/aiminminai/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><script>(() => {
  const isChatBtn = true
  const isChatHideShow = true

  if (isChatBtn) {
    const close = () => {
      Chatra('minimizeWidget')
      Chatra('hide')
    }

    const open = () => {
      Chatra('openChat', true)
      Chatra('show')
    }

    window.ChatraSetup = {
      startHidden: true
    }
  
    window.chatBtnFn = () => {
      const isShow = document.getElementById('chatra').classList.contains('chatra--expanded')
      isShow ? close() : open()
    }
  } else if (isChatHideShow) {
    window.chatBtn = {
      hide: () => {
        Chatra('hide')
      },
      show: () => {
        Chatra('show')
      }
    }
  }

  (function(d, w, c) {
    w.ChatraID = 't3Zijv5iKg5Z5HT6v'
    var s = d.createElement('script')
    w[c] = w[c] || function() {
        (w[c].q = w[c].q || []).push(arguments)
    }
    s.async = true
    s.src = 'https://call.chatra.io/chatra.js'
    if (d.head) d.head.appendChild(s)
  })(document, window, 'Chatra')

})()</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>